package com.tuju.currency

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.ashokvarma.bottomnavigation.BottomNavigationBar
import com.ashokvarma.bottomnavigation.BottomNavigationItem
import com.tuju.currency.base.BaseActivity
import com.tuju.currency.network.DefaultSubscriber
import com.tuju.currency.network.HttpClient
import com.tuju.currency.utils.showToast
import com.tuju.currency.view.home.HistoryFragment
import com.tuju.currency.view.home.HomeFragment
import com.tuju.currency.view.home.MeFragment
import com.tuju.currency.web.WebViewActivity
import kotlinx.android.synthetic.main.activity_main.*
import okhttp3.ResponseBody

class MainActivity : BaseActivity() {

    private var fragments: MutableList<Fragment> = ArrayList<Fragment>(2)
    private var homeFragment: Fragment? = null
    private var historyFragment: Fragment? = null
    private var mineFragment: Fragment? = null

    private val onlineUrl: String =
        "https://his.ipalmap.com/fever-inquiry/index.html?from=timeline#/index"
    private val zixunUrl: String =
        "https://h5.baike.qq.com/mobile/pneumonia.html?adtag=op.co.kdzfy.fygj&addressbar=hide&VNK=5a440c4f"
    private val dongtaiUrl: String =
        "https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_pc_3#tab0"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setStateBarColor(R.color.pink)
        initView()
    }

    private fun initView() {
        btn_test.setOnClickListener {
            WebViewActivity.loadUrl(
                this, onlineUrl
            )
        }

        btn_test2.setOnClickListener {
            HttpClient.getInstance().updateConfig()
                .subscribe(object : DefaultSubscriber<ResponseBody>() {
                    override fun _onError(errMsg: String?) {
                        showToast("请求失败${errMsg}")
                    }

                    override fun _onNext(entity: ResponseBody?) {
                        showToast("请求成功${entity?.string()}")
                    }
                })
        }

        //init bottomNavigatorBar
        bottom_navigation_bar.setMode(BottomNavigationBar.MODE_FIXED)
        bottom_navigation_bar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottom_navigation_bar
            .addItem(
                BottomNavigationItem(
                    R.drawable.home_,
                    "首页"
                ).setInactiveIconResource(R.drawable.home)
            )
            .addItem(
                BottomNavigationItem(
                    R.drawable.wenzhen_,
                    "问诊记录"
                ).setInactiveIconResource(R.drawable.wenzhen)
            )
            .addItem(
                BottomNavigationItem(
                    R.drawable.me_,
                    "个人中心"
                ).setInactiveIconResource(R.drawable.me)
            )
            //.setActiveColor("#f33035")        //设置添加选中时颜色
            .setFirstSelectedPosition(0)
            .initialise()
        bottom_navigation_bar.setTabSelectedListener(object :
            BottomNavigationBar.OnTabSelectedListener {
            override fun onTabReselected(position: Int) {
            }

            override fun onTabUnselected(position: Int) {
            }

            override fun onTabSelected(position: Int) {
                val fm = supportFragmentManager
                val transaction = fm.beginTransaction()
                hideFragment(transaction)
                when (position) {
                    0 -> {
                        if (homeFragment == null) {
                            homeFragment = HomeFragment.newInstance("首页")
                            transaction.add(R.id.fl_content, homeFragment!!)
                            fragments.add(homeFragment!!)
                        } else {
                            transaction.show(homeFragment!!)
                        }
                    }
                    1 -> {
                        if (historyFragment == null) {
                            historyFragment = HistoryFragment.newInstance(dongtaiUrl)
                            transaction.add(R.id.fl_content, historyFragment!!)
                            fragments.add(historyFragment!!)
                        } else {
                            transaction.show(historyFragment!!)
                        }
                    }
                    2 -> {
                        if (mineFragment == null) {
                            mineFragment = MeFragment.newInstance("")
                            transaction.add(R.id.fl_content, mineFragment!!)
                            fragments.add(mineFragment!!)
                        } else {
                            transaction.show(mineFragment!!)
                        }
                    }

                }
                transaction.commitAllowingStateLoss()
            }
        })
        setDefaultFragment()
    }

    private fun hideFragment(transaction: FragmentTransaction) {
        for (fragment in fragments) {
            transaction.hide(fragment)
        }
    }

    private fun setDefaultFragment() {
        val fm = supportFragmentManager
        val transaction = fm.beginTransaction()
        homeFragment = HomeFragment.newInstance("首页")
        fragments.add(homeFragment!!)
        transaction.replace(R.id.fl_content, homeFragment!!)
        transaction.commit()
    }
    
    companion object {
        @JvmStatic
        fun start(context: Context) {
            context.startActivity(Intent(context, MainActivity::class.java))
        }
    }

}
